package com.hbsx.why.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.hbsx.why.R;

/**
 * Created by liub
 * time 2017/8/29
 */

public abstract class PopWindowMine {
    private Context context;
    private PopupWindow popupWindowmine;
    private View view;

    // private OnItemClickListener listener;
    public PopWindowMine(Context context) {
        this.context = context;
        view = LayoutInflater.from(context).inflate(getLayoutManu(), null);
        // 设置 recyclerView
        getView(view);
        // popupWindow = new PopupWindow(view, 100, LayoutParams.WRAP_CONTENT);
        popupWindowmine = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        // context.getResources()
        // .getDimensionPixelSize(R.dimen.popmenu_width)
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景（很神奇的）
        popupWindowmine.setBackgroundDrawable(new BitmapDrawable());
        setBackgroundAlpha(0.5f, context);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha, Context mContext) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    // 下拉式 弹出 pop菜单 parent 右下角
    public void showAsDropDown() {
        // 使其聚集
        popupWindowmine.setFocusable(true);
        // 设置允许在外点击消失
        popupWindowmine.setOutsideTouchable(true);
        // // 刷新状态
        //popupWindow.update();
        popupWindowmine.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                dismiss();
            }
        });
        popupWindowmine.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    // 隐藏菜单
    public void dismiss() {
        setBackgroundAlpha(1f, context);
        popupWindowmine.dismiss();
    }


    public abstract int getLayoutManu();

    public abstract void getView(View view);
}
